package com.japancompany.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.japancompany.common.PageQuery;
import com.japancompany.common.PageResult;
import com.japancompany.common.Result;
import com.japancompany.entity.Article;
import com.japancompany.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/page")
    public Result<PageResult<Article>> getArticlePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Integer status) {
        
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        
        PageResult<Article> result = articleService.getArticlePage(pageQuery, title, categoryId, status);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<Article> getArticle(@PathVariable Long id) {
        Article article = articleService.getArticleDetail(id);
        if (article == null) {
            return Result.error("文章不存在");
        }
        return Result.success(article);
    }

    @PostMapping
    public Result<Boolean> createArticle(@RequestBody Article article) {
        boolean success = articleService.createArticle(article);
        return Result.success(success);
    }

    @PutMapping("/{id}")
    public Result<Boolean> updateArticle(@PathVariable Long id, @RequestBody Article article) {
        article.setId(id);
        boolean success = articleService.updateArticle(article);
        return Result.success(success);
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteArticle(@PathVariable Long id) {
        boolean success = articleService.deleteArticle(id);
        return Result.success(success);
    }

    @PutMapping("/{id}/status")
    public Result<Boolean> updateArticleStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = articleService.updateArticleStatus(id, status);
        return Result.success(success);
    }

    @GetMapping("/recommend")
    public Result<List<Article>> getRecommendArticles() {
        List<Article> articles = articleService.list(new QueryWrapper<Article>()
                .eq("deleted", 0)
                .eq("status", 1)
                .eq("is_recommend", 1)
                .orderByDesc("create_time")
                .last("LIMIT 5"));
        return Result.success(articles);
    }

    @GetMapping("/top")
    public Result<List<Article>> getTopArticles() {
        List<Article> articles = articleService.list(new QueryWrapper<Article>()
                .eq("deleted", 0)
                .eq("status", 1)
                .eq("is_top", 1)
                .orderByDesc("create_time")
                .last("LIMIT 5"));
        return Result.success(articles);
    }
}